package nsu.edu.zsq.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.slf4j.Slf4j;
import nsu.edu.zsq.exception.ParamException;

/** 
* Description: 全局异常处理<br>
*/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    
    /**
     * 参数验证异常（自定义异常）
     * @param request
     * @param response
     * @param ex
     * @return JsonData
     */
    @ExceptionHandler(value = ParamException.class)
    @ResponseBody
    public JsonData paramException(HttpServletRequest request, 
            HttpServletResponse response, Exception ex) {
        return JsonData.fail(ex.getMessage());
    }
    
    /**
     * 其它系统异常
     * @param request
     * @param response
     * @param ex
     * @return JsonData
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonData otherException(HttpServletRequest request, 
            HttpServletResponse response, Exception ex) {
        log.error("system error:{}",ex);
        return JsonData.fail("system error");
    }
}
